package cn.kai.org.controller;

import cn.kai.basic.exception.BusinessException;
import cn.kai.basic.util.ExcelUtil;
import cn.kai.org.domain.ShopAuditLog;
import cn.kai.org.service.IShopAuditLogService;
import cn.kai.org.service.IShopService;
import cn.kai.org.domain.Shop;
import cn.kai.org.query.ShopQuery;
import cn.kai.basic.util.AjaxResult;
import cn.kai.basic.util.PageList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

@RestController
@RequestMapping("/shop")
public class ShopController {
    @Autowired
    private IShopService shopService;

    @Autowired
    private IShopAuditLogService shopAuditLogService;

    /**
     * 导出excel
     * @param response
     */
    @GetMapping("/export")
    public void export( HttpServletResponse response){
        try {
            List<Shop> shops = shopService.queryAll();
            ExcelUtil.exportExcel(shops, null, "店铺信息", Shop.class, "shop.xls", response);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 店铺驳回
     * @param shopAuditLog
     * @return
     */
    @PostMapping("/reject")
    public AjaxResult reject(@RequestBody ShopAuditLog shopAuditLog){
        try {
            shopAuditLogService.reject(shopAuditLog);
            return AjaxResult.me();
        } catch (BusinessException e) {//自定义业务异常进行处理
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("保存对象失败！"+e.getMessage());
        }
    }

    /**
     * 激活店铺接口
     * @param shopId
     * @return
     */
    @GetMapping("/activation/{shopId}")
    public AjaxResult activation(@PathVariable("shopId") Long shopId, HttpServletResponse response){
        try {
            shopService.activation(shopId);
            //用户点击链接激活后跳转至激活页面
            response.sendRedirect("http://localhost:8081/#/activation");
            return AjaxResult.me();
        } catch (BusinessException e) {//自定义业务异常进行处理
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("保存对象失败！"+e.getMessage());
        }
    }

    /**
     * 审核通过
     * @return
     */
    @PostMapping("/pass")
    public AjaxResult shopPass(@RequestBody ShopAuditLog shopAuditLog){
        try {
            shopAuditLogService.pass(shopAuditLog);
            return AjaxResult.me();
        } catch (BusinessException e) {//自定义业务异常进行处理
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("保存对象失败！"+e.getMessage());
        }
    }

    /**
     * 店铺入驻
     * @param shop
     * @return
     */
    @PostMapping("/settlement")
    public AjaxResult settlement(@RequestBody Shop shop){
        try {
            shopService.settlement(shop);
            return AjaxResult.me();
        } catch (BusinessException e) {//自定义业务异常进行处理
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("保存对象失败！"+e.getMessage());
        }
    }

    /**
     * 保存和修改公用的
     * @param shop  传递的实体
     * @return Ajaxresult转换结果
     */
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody Shop shop){
        try {
            if( shop.getId()!=null)
                shopService.update(shop);
            else
                shopService.save(shop);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("保存对象失败！"+e.getMessage());
        }
    }
    /**
    * 删除对象信息
    * @param id
    * @return
    */
    @DeleteMapping(value="/{id}")
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            shopService.removeById(id);
            return AjaxResult.me();
        } catch (Exception e) {
        e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("删除对象失败！"+e.getMessage());
        }
    }
	
    //获取用户
    @GetMapping("/{id}")
    public Shop get(@PathVariable("id")Long id)
    {
        return shopService.queryById(id);
    }


    /**
    * 查看所有的员工信息
    * @return
    */
    @GetMapping()
    public List<Shop> list(){

        return shopService.queryAll();
    }


    /**
    * 分页查询数据
    *
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @PostMapping("/list")
    public PageList<Shop> json(@RequestBody ShopQuery query)
    {
        PageList<Shop> shopPageList = shopService.queryPage(query);
        return shopPageList;
    }
}
